package com.mybook.app.service;

import com.mybook.app.exception.DataIsEmptyException;
import com.mybook.app.mapper.CityInfoMapper;
import com.mybook.app.model.CityInfo;
import com.mybook.app.response.City;
import com.mybook.app.utils.Logger;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zjutsw on 2017/11/6.
 */
@Service
public class CommonService {
    @Autowired
    private CityInfoMapper cityInfoMapper;

    /***
     * 获取城市列表
     * @return
     */

    public List<City> city(String keyword) {
        List<City> cities = new ArrayList<>();
        List<CityInfo> cityInfos = cityInfoMapper.findCityInfoByCIdOrCName(keyword);

        if (CollectionUtils.isEmpty(cityInfos)) {
            Logger.info(this, "查询数据为空");
            throw new DataIsEmptyException();
        }
        //数据转化
        cities = cityInfos.stream().map(cityInfo -> {
            City city = new City();
            BeanUtils.copyProperties(cityInfo, city);
            return city;
        }).collect(Collectors.toList());

        return cities;

    }


}
